#include "mainwindow.h"

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{
    QAction* newAct = new QAction(tr("&New"), this);
    newAct->setShortcut(tr("Ctrl+N"));
    connect(newAct, SIGNAL(triggered()), this, SLOT(newGame()));
    QAction* pauseAct = new QAction(tr("&Pause"), this);
    pauseAct->setShortcut(tr("Ctrl+P"));
    connect(pauseAct, SIGNAL(triggered()), this, SLOT(pauseGame()));
    QAction* quitAct = new QAction(tr("&Quit"), this);
    quitAct->setShortcut(tr("Ctrl+Q"));
    connect(quitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
    QMenu* gameMenu = menuBar()->addMenu(tr("&Game"));
    gameMenu->addAction(newAct);
    gameMenu->addAction(pauseAct);
    gameMenu->addSeparator();
    gameMenu->addAction(quitAct);
    QMenu* helpMenu = menuBar()->addMenu("Help");
    QAction* aboutAct = new QAction(tr("&About"), this);
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
    helpMenu->addAction(aboutAct);
    //
    int cellLenght = 9;
    int cells_width = 40;
    int cells_height = 30;
    int canvas_width = cells_width*(cellLenght+1) + 1;
    int canvas_height = cells_height*(cellLenght+1) + 1;
    int win_width = canvas_width + 20;
    int win_height = canvas_height + 20;
    this->resize(win_width, win_height+50);
    //
    QWidget* central = new QWidget(this);
    setCentralWidget(central);
    setWindowTitle("Snake Game");
    //
    canvas = new Canvas(central);
    canvas->setGeometry(10, 10, canvas_width+4, canvas_height+4);
    control = new Control(canvas);
    control->setup(cells_width, cells_height, cellLenght);
    control->start();
    canvas->setControl(control);
    label = new QLabel(tr("Welcacome to Snake Game!!!") );
    statusBar()->addWidget(label);
    control->setLabel(label);
    control->setPauseAct(pauseAct);
}


void MainWindow::closeEvent(QCloseEvent *e)
{
    control->closeAllWindows();
}

void MainWindow::newGame()
{
    control->start();
    label->setText("Welcome to Snake Game!!!");
}

void MainWindow::pauseGame()
{
    control->tooglePause();
}

void MainWindow::about()
{
    control->about();
}
